package org.example

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.time.Time

/**
 * description ：wordCount
 * author      ：剧情再美终是戏 
 * mail        : 13286520398@163.com
 * date        ：Created in 2020/2/19 11:31
 * modified By ：
 * version:    : 1.0
 */
object WordCount {

  def main(args: Array[String]): Unit = {
    // 创建运行环境
    val environment = StreamExecutionEnvironment.getExecutionEnvironment
    environment.setParallelism(1)

    // 获取数据
    val value = environment.socketTextStream("hadoop101", 9999)


    // 处理数据
    val result = value
      .flatMap(_.split("\\s"))
      .map(l => WordCount(l, 1))
      .keyBy("word")
      .timeWindow(Time.seconds(5))
      .sum("count")

    // 打印
    result.print()

    // 执行
    environment.execute("Flink Streaming Java API Skeleton");
  }

  case class WordCount(word: String, count: Long)

}
